#include "PlayerStateDash.h"
#include "Player.h"

PlayerStateDash::PlayerStateDash(Player* player, sf::Sound* dashSFX) : 
PlayerState(player),
_dashSFX(dashSFX)
{}

PlayerStateDash::~PlayerStateDash(void){}

void PlayerStateDash::activate()
{
	_player->dash();
	_player->getAnimationMgr().setAnimation("dash");
	_dashDirection = _player->getFlip();
	_resetDash = true;
	_dashTimer.restart();
	_dashSFX->play();
}

void PlayerStateDash::deactivate()
{
	if (_resetDash)
		_player->resetDash();
	if (_dashSFX->Playing)
		_dashSFX->stop();
}

int PlayerStateDash::update()
{
	const sf::Int32 maxDashLength = 700;

	if (_player->damageReceived())
		return PSRV_TAKE_DAMAGE;

	if (_dashDirection == 1.0f && _player->getFlip() == -1.0f)
	{
		return PSRV_MOVING_GROUND;
	}
	else if (_dashDirection == -1.0f && _player->getFlip() == 1.0f)
	{
		return PSRV_MOVING_GROUND;
	}
	if (_player->getControlState()->isKeyPressed(CControls::Key(CTRL_JUMP, 0), 0))
	{
		_player->_jumpStartPosition = _player->_position.y;
		_resetDash = false;
		return PSRV_JUMPING;
	}

	if (_player->getControlState()->isKeyPressed(CControls::Key(CTRL_SHOOT, 0), 0))
	{
		return PSRV_ATTACK;
	}

	if (_player->getAnimationMgr().getCurrentID() == "dash")
	{
		if (_player->getAnimationMgr().getCurrentFrame() > 1)
		{
			_player->applyVelocity(VA_FLIP_BASED);
		}
	}
	if (_player->getControlState()->isKeyReleased(CControls::Key(CTRL_DASH, 0), 0))
	{
		return PSRV_DASH_RETRACT;
	}

	if (_dashTimer.getElapsedTime().asMilliseconds() >= _player->getDashLength())
	{
		return PSRV_DASH_RETRACT;
	}

	if (((_player->flags() & COLLISION_HIT_LEFT) == COLLISION_HIT_LEFT) || ((_player->flags() & COLLISION_HIT_RIGHT) == COLLISION_HIT_RIGHT))
	{
		return PSRV_DASH_RETRACT;
	}

	if (!_player->_onGround)
		return PSRV_FALLING;

	return PSRV_NOTHING;
}